<?php
/**
* @author Oliver Leisalu
* @package Ipx
*/
class IpxSms {
	
	private static $smsInfo;
	
	private static $logEntry;
	
	private static $configuration;
	
	/**
	 * @return IpxSmsInfo
	 */
	public static function getInfo() {
		IpxSmsLogEntry::getTable()->update();
		
		if( !ereg('217\.151\.193\..*',ip() ) && $_REQUEST['debug'] != 1 )
			die('Not authorized!');
			
		if( !$_GET['OriginatorAddress'] )
			die('Not authorized');
		
		require( DOM_CONF_DIR_PATH . 'ipx.conf.php' );
		self::$configuration = $IPX['sms'];
		
		$o = new IpxSmsInfo();
		$o->phoneNr = $_GET['OriginatorAddress'];
		$o->destinationPhoneNr = $_GET['DestinationAddress'];
		$o->message = $_GET['Message'];
		$o->id = $_GET['MessageId'];
		$o->operator = $_GET['Operator'];
		$o->timeStamp = $_GET['TimeStamp'];
		
		self::$smsInfo = $o;
		
		self::log();
		
		return $o;
	}
	
	public static function echoAcknowledgement() {
		header('Content-Type: text/plain');
		echo '<DeliveryResponse ack="true"/>';
	}
	
	public static function log() {
		$log = new IpxSmsLogEntry();
		$log->datetime = 'now';
		$log->ip = ip();
		$log->contents = self::$smsInfo->message;
		$log->price = self::$configuration['destinationPhoneNrPrices'][self::$smsInfo->destinationPhoneNr];
		$log->operator = strtolower(self::$smsInfo->operator);
		$log->phoneNr = self::$smsInfo->phoneNr;
		$log->ipxId = self::$smsInfo->id;
		$log->replaySmsStatus = 0;
		
		if( !$log->save() ) 
			throw new RuntimeException('Failed saving IpxSms log entry. Reasons: '.implode(',',$log->getValidationMessages()) );
		
		self::$logEntry = $log;
	}
	
	public static function sendReplaySms( $message ) {
			
		self::$logEntry->replayContents = $message;
		
		$request = array(
			'correlationId' => '10010', 
			'originatingAddress' => self::$smsInfo->phoneNr,
			'userDataHeader' => '#NULL#',
			'DCS' => 17,
			'PID' => -1,
			'relativeValidityTime' => 172800,
			'deliveryTime' => '#NULL#',
			'statusReportFlags' => 1,
			'accountName' => '#NULL#',
			'VAT' => -1,
			'contentCategory' => '#NULL#',
			'contentMetaData' => '#NULL#',
			'originatorTON' => 0, 
			'destinationAddress'=> self::$smsInfo->destinationPhoneNr, 
			'tariffClass' => 'EEK0', 
			'userData' => $message,
			'username' => self::$configuration['username'],
			'password' => self::$configuration['password'],
			'referenceId' => self::$smsInfo->id,
		);
		
		$sc = new SoapClient('http://europe.ipx.com/api/services2/SmsApi51?wsdl');
		$result = $sc->send( $request );

		if( $result->responseCode == 0 )
			self::$logEntry->replaySmsStatus = 2;
		else
			self::$logEntry->replaySmsStatus = 3;

		if( !self::$logEntry->save() ) 
			throw new RuntimeException('Failed saving IpxSms log entry. Reasons: '.implode(',',self::$logEntry->getValidationMessages()) );
	}
	
	public static function logReplaySmsDelivery() {
		$entry = IpxSmsLogEntry::query()->where('ipxId = \''.addslashes($_REQUEST['MessageId']).'\'')->limit(1)->commit();
		$entry->replaySmsStatus = 1;
		if( !$entry->save() )
			throw new RuntimeException('Failed saving IpxSms log entry. Reasons: '.implode(',',$entry->getValidationMessages()) );
	}
	
}
?>